package com.itguigu.process.controller;

import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itguigu.common.result.Result;
import com.itguigu.model.process.ProcessType;
import com.itguigu.process.service.ProcessTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 *
 */
@Api(value = "审批类型" , tags = "审批类型")
@RestController
@RequestMapping(value = "/admin/process/processType")
@Slf4j
public class ProcessTypeController {

    @Resource
    private ProcessTypeService processTypeService;


    // 查询所有审批分类
    @ApiOperation("查询所有type")
    @GetMapping("findAll")
    public Result findAll() {
        return Result.success(processTypeService.list());
    }

    // 分页查询
    /**
     * 分页查询
     * PreAuthorize : 权限
     * @param page
     * @param limit
     * @return
     */
    @PreAuthorize("hasAuthority('bnt.processType.list')")
    @ApiOperation(value = "获取分页列表")
    @GetMapping("{page}/{limit}")
    public Result index(@PathVariable Long page,
                        @PathVariable Long limit){
        log.info("process分页查询 , page : {} , limit : {}" , page , limit);
        Page<ProcessType> pageParam = new Page<>(page, limit);
        IPage<ProcessType> pageModel = processTypeService.page(pageParam);
        return Result.success(pageModel);
    }


    @PreAuthorize("hasAuthority('bnt.processType.add')")
    @ApiOperation(value = "新增process流程")
    @PostMapping("save")
    public Result save(@RequestBody ProcessType processType) {
        log.info("新增的 process 内容 : {}" , processType);
        boolean save = processTypeService.save(processType);
        return Result.success();
    }


    @PreAuthorize("hasAuthority('bnt.processType.remove')")
    @ApiOperation(value = "删除process流程")
    @DeleteMapping("{remove/{id}")
    public Result remove(@PathVariable Long id) {
        log.info("要删除process流程的id : {} " , id);
        boolean b = processTypeService.removeById(id);
        return Result.success();
    }

    @PreAuthorize("hasAuthority('bnt.processType.update')")
    @ApiOperation(value = "修改process")
    @PutMapping("/update")
    public Result updateById(@RequestBody ProcessType processType) {
        log.info("要修改的process 的id是 : {} . 内容为: {}" , processType.getId() , processType);
        boolean b = processTypeService.updateById(processType);
        return Result.success();
    }


    @PreAuthorize("hasAuthority('bnt.processType.list')")
    @ApiOperation(value = "根据id获取")
    @GetMapping("/get/{id}")
    public Result get(@PathVariable Long id) {
        log.info("要查询的process流程的id为 : {}" , id);
        ProcessType processType = processTypeService.getById(id);
        return Result.success(processType);
    }




}
